#! /bin/bash

# Usage:
#
# ./uploader USERNAME/EMAIL PASSWORD FILE_LIST
#



# extracts the value of a name="value" pair, from within a string
# @param 1  the entire string
# @param 2  a string indicating the named value to extract
function extract {
    str=$1
    param=$2

    str=${str##*${param}=\"}
    echo ${str%%\"*}
}

function url_encode {
    str=$1

    str=`echo $str | sed -e 's/@/%40/g'`
    str=`echo $str | sed -e 's/+/%2B/g'`
    str=`echo $str | sed -e 's/:/%3A/g'`
    str=`echo $str | sed -e 's/;/%3B/g'`
    str=`echo $str | sed -e 's/&/%26/g'`
    str=`echo $str | sed -e 's/,/%2C/g'`
    str=`echo $str | sed -e 's/=/%3D/g'`
    str=`echo $str | sed -e 's/?/%3F/g'`

    echo $str
}

function login {

    login=`url_encode $1`
    pass=$2
    site=$3

    tmp_file_prefix=/tmp/net.twingear.uploader.
    login_html=${tmp_file_prefix}login.html
    grep_html=${tmp_file_prefix}grep.html
    out_html=${tmp_file_prefix}out.html
    cookies_file=${tmp_file_prefix}cookies

    # download the login page
    curl -s http://${site}.pnimedia.com/home.aspx > $login_html

    # search for all hidden input types
    grep -i -A 0 -B 0 type=\"hidden\" $login_html > $grep_html

    # get a line count (cut: drops the filename)
    n=`wc -l $grep_html | cut -d ' ' -f 1`

    params=''

    for i in `seq 1 $n`;
    do
        line=`cat $grep_html | head -n $i | tail -n 1`
        
        name=`extract "$line" name`
        value=`extract "$line" value`

        name=`url_encode $name`
        value=`url_encode $value`

        params="${params} -d $name=$value"
    done

    email_field=`grep -i -A 0 -B 0 type=\"text\" $login_html`
    pass_field=`grep -i -A 0 -B 0 type=\"password\" $login_html`

    email_field=`extract "$email_field" name`
    pass_field=`extract "$pass_field" name`

    email_field=`url_encode $email_field`
    pass_field=`url_encode $pass_field`

    params="${params} -d $email_field=${login}"
    params="${params} -d $pass_field=${pass}"

    curl -s -c $cookies_file $params -d Navigation1%3AsignInButton.x=0 -d Navigation1%3AsignInButton.y=0 -d loginButton.x=0 -d loginButton.y=0 http://${site}.pnimedia.com/home.aspx > $out_html

    curl -s -b $cookies_file http://${site}.pnimedia.com/upload/upload_simple.aspx > $out_html


    #TODO: needs to be more robust, assumes userID and appContextID inputs are on separate lines

    # output the user id
    line=`grep -i -A 0 -B 0 userID $out_html`
    echo `extract "$line" value`

    # output the context id
    line=`grep -i -A 0 -B 0 appContextID $out_html`
    echo `extract "$line" value`

    rm ${tmp_file_prefix}*
}


login=$1
pass=$2

shift
shift

# login and obtain the user id and context id
data=`login $login $pass walmart`
user=`echo $data | cut -d ' ' -f 1` # first parameter from login.sh is the user id
ctxtID=`echo $data | cut -d ' ' -f 2` # second parameter from login.sh is the context id

# begin uploading
n=1
total=$#

until [ -z "${1}" ]
do

    echo Uploading file ${n}/${total}: $1

	curl -s -F "appContextID=${ctxtID}" -F "userID=${user}" -F "albumID=" -F "num=1" -F "file1=@${1}" -F "name1=" http://uploads.pnimedia.com/browse_upload_recx.aspx > /dev/null
	
	let "n += 1"
	shift
done

echo Upload complete
